package edu.maxflow;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.hadoop.io.Writable;

public class Path implements Writable {

	private List<Edge> edgeList = new ArrayList<Edge>();

	public boolean containsEdge(Edge edgeToSearch) {
		for (Edge edge : edgeList) {
			if (edgeToSearch.equals(edge)) {
				return true;
			}
		}

		return false;
	}

	public boolean containsSameEdge() {
		for (int i = 0; i < edgeList.size(); i++) {
			for (int j = i + 1; j < edgeList.size(); j++) {
				if (edgeList.get(i).equals(edgeList.get(j))) {
					return true;
				}
			}
		}

		return false;
	}

	public void readFields(DataInput dataInput) throws IOException {
		edgeList.clear();

		int size = dataInput.readInt();
		if (size != 0) {
			edgeList = new ArrayList<Edge>(size);
			for (int i = 0; i < size; i++) {
				Edge edge = new Edge();
				edge.readFields(dataInput);
				edgeList.add(edge);
			}
		}
	}

	public void write(DataOutput dataOutput) throws IOException {
		if (edgeList != null) {
			dataOutput.writeInt(edgeList.size());
			for (Edge edge : edgeList) {
				edge.write(dataOutput);
			}
		} else {
			dataOutput.writeInt(0);
		}
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("Path [edgeList=");
		builder.append(edgeList);
		builder.append("]");
		return builder.toString();
	}

	public List<Edge> getEdgeList() {
		return edgeList;
	}

	public void setEdgeList(List<Edge> edgeList) {
		this.edgeList = edgeList;
	}
}
